#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
using namespace std;

void Serialize()
{
  const char* name = "六盘水";
  int age =20;
  float score[] = {66.5,77.5,88};
  Json::Value val;
  val["姓名"] = name;
  val["年龄"] = age;
  val["成绩"].append(score[0]);
  val["成绩"].append(score[1]);
  val["成绩"].append(score[2]);
  std::cout << "我想打印Value对象:\n" << val << std::endl;

  Json::StyledWriter writer;
  std::string str = writer.write(val);
  std::cout << "我是Json格式的字符串:\n" << str << std::endl;
}

void UnSerialize()
{
  std::string str = R"({"姓名":"周宇航", "年龄":20, "成绩":[77,88,99]})";
  std::cout << str << std::endl;
  Json::Reader reader;
  Json::Value val;
  bool ret = reader.parse(str, val);
  if(ret == false){
    std::cout << "json parse erroe\n";
    return ;
  }
  std::cout << "我是asString后的字符串: " << val["姓名"].asString() << std::endl;
  std::cout << "我是Value对象的值: " << val["姓名"] << std::endl;

  std::cout << val["年龄"].asInt() << std::endl;
  if(val["成绩"].isArray()){
    int sz = val["成绩"].size();
    for (int i = 0; i < sz; ++i){
      std::cout << val["成绩"][i].asFloat() << std::endl;
    }
  }
  return ;
}

int main()
{
  Serialize();
  UnSerialize();  
  return 0;
}
